package com.myacg.platform.ui.event.move;

import java.awt.Component;

import com.myacg.core.entity.Point;
import com.myacg.core.move.CircularMovePlugin;
import com.myacg.core.move.MoveAdapter;
import com.myacg.core.move.MoveEngine;
import com.myacg.core.move.MoveListener;
import com.myacg.core.move.MovePlugin;
import com.myacg.core.utils.MoveUtils;

public abstract class BaseCircularMoveToEvent implements ComponentMoveToEvent{
    protected Component comp;
    protected MoveListener movelistener;
    protected MoveEngine moveEngine;
    protected MovePlugin movePlugin;
    
	public BaseCircularMoveToEvent(Component comp){
		 this.comp=comp;
		 initMoveLinstener();//初始化事件监听器
	     java.awt.Point point = comp.getLocation();
	     CircularMovePlugin movePlug = new CircularMovePlugin(new Point(point.x, point.y));
	     this.moveEngine= new MoveEngine(movePlug, movelistener);
		 //获取移动插件
		 this.movePlugin=moveEngine.getMovePlugin();
		 //暂停
		 this.moveEngine.pause();
	}
	@Override
	public void moveTo(Point point, double F, double v0) {
    	//计算目标点和方向
    	Point selfPoint = new Point(comp.getLocation().x, comp.getLocation().y);
    	//获取与目标点之间的差距
    	double length = MoveUtils.getRangle(selfPoint, point);
    	//获取角度
    	if(length!=0){
    		movePlugin.setStartPoint(point);
    		//重设引擎内核起始点
    		Point p =movePlugin.getPoint();
    		p.x=selfPoint.x;
    		p.y=selfPoint.y;
    		//重置运行状态
        	movePlugin.resetState();
        	//给予某个速度点
        	movePlugin.setF(F);
    		movePlugin.setV0(v0);
    		//启动
    		this.moveEngine.keep();
    	}
	}

	protected void pauseStop(){
		
	}
	
	protected abstract void OutOffByRunBefor();
	
	
	// 在运算结束时候调用的方法
	protected abstract void OutOffByRunAfter();
 
	private void initMoveLinstener(){
	    System.out.println("初始化移动监听器 ，实现基于移动的事件开发");
		movelistener=new MoveAdapter(){
			@Override
			public void runBefore() {
				OutOffByRunBefor();
			}
			@Override
			public void runAfter() {
				// 设置运算后的控件的位置
				Point p = movePlugin.getPoint();
				comp.setLocation(p.x, p.y);

				// 在运算结束时候调用的方法
				OutOffByRunAfter();
			}
			@Override
			public void whenPause() {
				pauseStop();
			}
		};
		
		 
	}
	
	public void distoray(){
		this.moveEngine.distoraySelf();
	}
}
